/*
 *  Class name: LengthLimiterDocument.java
 *  Version: 1.0
 *  Date: 7.6.2014
 */
package cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Michal Ďuriš
 */
public class LengthLimiterDocument extends PlainDocument {

    private static final long serialVersionUID = 1L;

    private final int limit;

    public LengthLimiterDocument(int limit) {
        this.limit = limit;
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a) throws
            BadLocationException {

        if (str == null) {
            return;
        }

        if (getLength() + str.length() <= limit) {
            super.insertString(offs, str, a);
        }
    }

}
